<?php
	/*
 	* @version $Id: exchange.php,v 1.0.1 2011/05/18 16:29:56 marco_manti
 	* @package http://www.gisa.by
 	* @copyright (C) 2006 - 2011 marco_manti(R)
 	* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 	* @description Script replace date fields for some database objects
 	*
 	*/

        // get library
	require_once(dirname(dirname(__FILE__)).'/includes/library.php');

        // init global classes
        $config = new CONFIG();
        $dbo = new DB();
        $helper = new HELPER();

        // globals
        $endline = chr(13).chr(10);
        $counter = 0;
        $today = date('Y-m-d H:i');
        $title = ' UPDATE EXCHANGE RATES: ';
        $email_title = 'GISA.by ::' . $title;
        $email = $config->server_root;
        $day  = strtotime($today);
        $day += 60 * 60 * 24 + 1;
        $tomorow = date('Y-m-d', $day);
        $logmsg = $mysqlerrlog = '';

        // mail headers
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=windows-1251\r\n";
        $headers .= "From: GISA.by <gisaby@gmail.com>\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();

        // connect to db & get data
	if (!$dbo->connect()) {
            $logmsg .= $today.$title.'ERROR - connect to database ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - connect to database ['.$dbo->error().'];', $headers);
        }

        try {
            // setup curl options
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_FAILONERROR, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
            curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); // return into a variable

            // get exchange rates
            curl_setopt($curl, CURLOPT_URL, 'http://www.obmennik.by/xml/'); // set url to post to
            $xml_data = curl_exec($curl); // run the whole process
        } catch(Exception $e) {
            $logmsg .= $today.$title.'ERROR - get data via curl ['.$e->getMessage().'];'.$endline;
            mail($email, $email_title, 'ERROR - get data via curl ['.$e->getMessage().'];', $headers);
        }
        
        // convert xml data
        libxml_use_internal_errors(true);
        $objects = simplexml_load_string($xml_data);
        
        if (count($objects) > 0) {
            // get agregate exchange rates
            $usd_rate = $eur_rate = $rur_rate = 0;
            foreach ($objects as $object) {
                $usd_rate += ($object->usd->sell + $object->usd->buy) / 2;
                $eur_rate += ($object->eur->sell + $object->eur->buy) / 2;
                $rur_rate += ($object->rur->sell + $object->rur->buy) / 2;
                $counter++;
            }
            $usd_rate /= $counter;
            $eur_rate /= $counter;
            $rur_rate /= $counter;

            // conversion to abs values
            $byr_rate = $usd_rate;
            $usd_rate = $byr_rate / $usd_rate;
            $eur_rate = $byr_rate / $eur_rate;
            $rur_rate = $byr_rate / $rur_rate;

            $query = " INSERT INTO rlt_exchange_rates "
                   . " (usd, eur, byr, rur) "
                   . " VALUES ($usd_rate, $eur_rate, $byr_rate, $rur_rate) ";

            $rows = $dbo->query($query);
            if ($dbo->_lastResult < 0) {
                $logmsg .= $today.$title.'ERROR - insert fields ['.$dbo->error().'];'.$endline;
                $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
                mail($email, $email_title, 'ERROR - insert fields ['.$dbo->error().'];', $headers);
            } else {
                $logmsg .= $today.$title.'SUCCESS - exchange rates inserted;'.$endline;
            }
        } else {
            $errors = implode('; ', libxml_get_errors());
            $logmsg .= $today.$title.'ERROR - convert XML data ['.$errors.'];'.$endline;
            mail($email, $email_title, 'ERROR - convert XML data ['.$errors.'];', $headers);
        }
        
        // write state to log
        $helper->writeToLog($logmsg);

        // write SQL error if present
        if ($mysqlerrlog != '') {
            $helper->writeToFile($mysqlerrlog, 'a+', 'mysql_errors.log');
        }
?>